package com.ktjiaoyu.ktmall.dao.impl;

import com.ktjiaoyu.ktmall.dao.BaseDao;
import com.ktjiaoyu.ktmall.dao.CategoryDao;
import com.ktjiaoyu.ktmall.entity.Category;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xm
 * @date 2025/9/10
 * @Dome
 */
public class CategoryDaoImpl extends BaseDao implements CategoryDao {
    @Override
    public List<Category> findAll() {
        String sql = "SELECT id,name,parent_id,icon,sort,`status`,is_deleted FROM kt_category WHERE is_deleted=0 ORDER BY parent_id,sort ASC";
        try {
            return executeQuery(sql, rs -> {
                List<Category> list = new ArrayList<>();
                try {
                    while (rs.next()) {
                        Category category = new Category();
                        category.setId(rs.getString("id"));
                        category.setName(rs.getString("name"));
                        category.setParentId(rs.getString("parent_id"));
                        category.setIcon(rs.getString("icon"));
                        category.setSort(rs.getInt("sort"));
                        category.setStatus(rs.getInt("status"));
                        category.setIsDeleted(rs.getInt("is_deleted"));
                        list.add(category);
                    }
                    return list;
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            });
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(Category category) {
        String sql = "insert into kt_category(name,parent_id,icon,sort,status) values(?,?,?,?,?)";
        try {
            return executeUpdate(sql, category.getName(), category.getParentId(), category.getIcon(), category.getSort(), category.getStatus());
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int update(Category category) {
        StringBuffer sql = new StringBuffer("update kt_category set ");
        List<Object> params = new ArrayList<>();
        if (category.getName() != null) {
            sql.append("name=?,");
            params.add(category.getName());
        }
        if (category.getParentId() != null) {
            sql.append("parent_id=?,");
            params.add(category.getParentId());
        }
        if (category.getIcon() != null) {
            sql.append("icon=?,");
            params.add(category.getIcon());
        }
        if (category.getSort() != null) {
            sql.append("sort=?,");
            params.add(category.getSort());
        }
        if (category.getStatus() != null) {
            sql.append("status=?,");
            params.add(category.getStatus());
        }
        sql.append(" update_time=now() where id=?");
        params.add(category.getId());
        try {
            return executeUpdate(sql.toString(), params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int delete(Long id) {
        String sql = "update kt_category set is_deleted=1 where id=?";
        try {
            return executeUpdate(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }
}
